<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2003-2005 Frederic Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-2012 Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

include "db_settings.inc";

class DBSortOrder {
    const asc = "ASC";
    const desc = "DESC";
    
    private function __construct() {
    }
}

class ConfigDAO {
	private $config;
	
	public function __construct() {
		$this->init();
	}
	
	private function init() {
		$this->config = array();
		$sql = "SELECT `key`, `value` FROM config";
		$exe = biblio_db_query($sql);
		if ($exe !== FALSE) {
			while (($row = biblio_db_fetch_array($exe)) !== FALSE) {
				$this->config[$row['key']] = $row['value'];
			}
		}	
	}
	
	public function get($key) {
		if (isset($this->config[$key]))
			return $this->config[$key];
		else
			return FALSE;
	}
	
	public function set($key, $value) {
		if (!empty($key))
			$this->config[$key] = $value;
		return $this;
	}
	
	public function write() {
		foreach ($this->config as $key => $value) {
			$sql = "REPLACE INTO config (`key`, `value`) VALUES ('$key', '$value')";
			if (biblio_db_query($sql) === FALSE)
				return FALSE;
		}
		return $this;
	}
}

function put_log($type, $requete, $user)
{
	static $types = array('', "SEARCH", "ADD", "MODIF", "DELETE", 
			"ADMIN", "OTHER", "HISTORY", "STATS");
	$date_log =date ("Y-M-d : H:i");
	$log_path=get_logpath();
	if (($type > 0) && ($type < 9))
		$type_log = $types[$type];
	else
		$type_log = "";
	if ($user)
		$str_log = "($user)";
	else 
		$str_log = ""; 
	$LOG = @fopen($log_path."biblio.log", "a");
	if ($LOG !== false) {
		fputs($LOG, "$date_log $str_log : $type_log => $requete\n");
		fclose($LOG);
	}
}

function biblio_db_connect($hostname, $port, $username, $password) {
    $db = @mysql_connect ($hostname.":".$port, $username , $password);
    return $db;
}

function biblio_db_pconnect($hostname, $port, $username,$password) {
    $db = @mysql_pconnect ($hostname.":".$port, $username , $password);
    return $db;
}

function biblio_db_select_db($db_name = DB_DB_NAME, $link = FALSE) {
    global $db;
    if ($link !== FALSE)
		return mysql_select_db($db_name, $link);
    else
		return mysql_select_db($db_name, $db);
}

function biblio_db_query( $query ,$link = FALSE) {
    global $db;
    if ($link !== FALSE)
		return mysql_query( $query ,$link);
    else
		return mysql_query( $query ,$db);
}

function biblio_db_fetch_array($result, $result_type = MYSQL_ASSOC) {
    return mysql_fetch_array($result, $result_type);
}

function biblio_db_fetch_row($result) {
    return mysql_fetch_row($result);
}

function biblio_db_fetch_object($result) {
    return mysql_fetch_object($result);
}

function biblio_next_object($result) {
    return mysql_fetch_object($result);
}

function biblio_db_num_rows($result) {
    return mysql_num_rows( $result );
}

function biblio_db_num_fields($result) {
    return mysql_num_fields($result);
}

function biblio_db_field_name( $result , $index ) {
    return mysql_field_name( $result , $index );
}

function biblio_db_errno($link = FALSE) {
    global $db;
    if ($link === FALSE)
		return mysql_errno($db);
    else
		return mysql_errno($link);
}

function biblio_db_error($link = FALSE) {
    global $db;
    if ($link === FALSE)
		return mysql_error($db);
    else
		return mysql_error($link);
}

function biblio_db_insert_id($link = FALSE) {
    global $db;
    if ($link === FALSE)
		return mysql_insert_id($db);
    else
		return mysql_insert_id($link);
}

function biblio_db_free_result($result) {
    return mysql_free_result($result);
}

function biblio_db_close($link = FALSE) {
    global $db;
    if ($link === FALSE)
		return mysql_close($db);
    else
		return mysql_close($link);
}

$db = biblio_db_connect(DB_HOSTNAME, DB_PORT, DB_USERNAME , DB_PASSWORD);
if (!$db)
    die("Unable to connect to SQL server");
if (!biblio_db_select_db(DB_DB_NAME, $db))
    die("Unable to select database");
$dbConfig = new ConfigDAO();

?>
